<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="search">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
          <el-form-item label="用户ID" prop="userId">
            <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="金币" prop="goldRange">
            <el-input v-model="queryParams.params.beginGold" placeholder="最小值" clearable style="width: 115px" @keyup.enter="handleQuery" />
            <span style="margin: 0 10px">至</span>
            <el-input v-model="queryParams.params.endGold" placeholder="最大值" clearable style="width: 115px" @keyup.enter="handleQuery" />
          </el-form-item>

          <el-form-item label="钻石" prop="diamond">
            <el-input v-model="queryParams.params.beginDiamond" placeholder="最小值" clearable style="width: 115px" @keyup.enter="handleQuery" />
            <span style="margin: 0 10px">至</span>
            <el-input v-model="queryParams.params.endDiamond" placeholder="最大值" clearable style="width: 115px" @keyup.enter="handleQuery" />
          </el-form-item>

          <el-form-item label="硬币" prop="rankCoin">
            <el-input v-model="queryParams.params.beginRankCoin" placeholder="最小值" clearable style="width: 115px" @keyup.enter="handleQuery" />
            <span style="margin: 0 10px">至</span>
            <el-input v-model="queryParams.params.endRankCoin" placeholder="最大值" clearable style="width: 115px" @keyup.enter="handleQuery" />
          </el-form-item>


          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button v-hasPermi="['mushcoin:userGameCurrency:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="userGameCurrencyList" @selection-change="handleSelectionChange" @sort-change="handleSortChange">
        <el-table-column type="selection" width="55" align="center" />
<!--        <el-table-column v-if="true" label="ID" align="center" prop="id" />-->
        <el-table-column label="TG用户ID" align="center" prop="userId" />
        <el-table-column label="姓名" align="center" prop="realName"  :show-overflow-tooltip='true'/>
        <el-table-column label="1金币" align="center" prop="gold" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="2钻石" align="center" prop="diamond" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="201硬币" align="center" prop="rankCoin" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="mushcoinGame" align="center" prop="mushcoin" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="mushcoin_h5" align="center" prop="inviteRewardsTotal" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="11Trait" align="center" prop="trait" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="12Relic" align="center" prop="relic" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="22ToyChips" align="center" prop="toyChips" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="100cube" align="center" prop="cube100" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="101pickaxs" align="center" prop="pickaxs" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="102drills" align="center" prop="drills" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="103bombs" align="center" prop="bombs" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="3001BlueRunes" align="center" prop="blueRunes" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="3002PinkRunes" align="center" prop="pinkRunes" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="3003YellowRunes" align="center" prop="yellowRunes" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true'/>
        <el-table-column label="BossRush4" align="center" prop="bossRush" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="GoldRush5" align="center" prop="goldRush" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="GaveRush6" align="center" prop="gaveRush" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="DwarvenKing7" align="center" prop="dwarvenKing" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="HeroLegion8" align="center" prop="heroLegion" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="key9" align="center" prop="key9" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="key10" align="center" prop="key10" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="key13" align="center" prop="key13" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="key20" align="center" prop="key20" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="key21" align="center" prop="key21" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="key2000" align="center" prop="key2000" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="key2001" align="center" prop="key2001" sortable="custom" :sort-orders="['ascending', 'descending']" :show-overflow-tooltip='true' />
        <el-table-column label="创建日期" align="center" prop="createTime" width="180" sortable="custom" :sort-orders="['ascending', 'descending']">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import {
  listUserGameCurrency,
  getUserGameCurrency,
  delUserGameCurrency,
  addUserGameCurrency,
  updateUserGameCurrency
} from '@/api/mushcoin/userGameCurrency';
import { UserGameCurrencyVO, UserGameCurrencyQuery, UserGameCurrencyForm } from '@/api/mushcoin/userGameCurrency/types';
import { globalHeaders } from '@/utils/request';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

// 表格数据
const userGameCurrencyList = ref<UserGameCurrencyVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
// 选中数组
const ids = ref<Array<string | number>>([]);
// 非单个禁用
const single = ref(true);
// 非多个禁用
const multiple = ref(true);
// 总条数
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const userGameCurrencyFormRef = ref<ElFormInstance>();
const uploadRef = ref<ElUploadInstance>();

/*** 导入参数 */
const upload = reactive<ImportOption>({
  // 是否显示弹出层（导入）
  open: false,
  // 弹出层标题（导入）
  title: '',
  // 是否禁用上传
  isUploading: false,
  // 是否更新已经存在的用户数据
  updateSupport: 1,
  // 设置上传的请求头部
  headers: globalHeaders(),
  // 上传的地址
  url: import.meta.env.VITE_APP_BASE_API + '/mushcoin/userGameCurrency/importData'
});

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: UserGameCurrencyForm = {};
// 默认排序
const defaultSort = ref<any>({ prop: 'createTime', order: 'descending' });
const data = reactive<PageData<UserGameCurrencyForm, UserGameCurrencyQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 50,
    userId: undefined,
    gold: undefined,
    diamond: undefined,
    rankCoin: undefined,
    orderByColumn: defaultSort.value.prop,
    isAsc: defaultSort.value.order,
    params: {
      beginGold: undefined,
      endGold: undefined,
      beginDiamond: undefined,
      endDiamond: undefined,
      beginRankCoin: undefined,
      endRankCoin: undefined
    }
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询TG用户游戏币列表 */
const getList = async () => {
  loading.value = true;
  const res = await listUserGameCurrency(queryParams.value);
  userGameCurrencyList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  userGameCurrencyFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: UserGameCurrencyVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加TG用户游戏币';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: UserGameCurrencyVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getUserGameCurrency(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改TG用户游戏币';
};

/** 提交按钮 */
const submitForm = () => {
  userGameCurrencyFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateUserGameCurrency(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addUserGameCurrency(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: UserGameCurrencyVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除TG用户游戏币编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delUserGameCurrency(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'mushcoin/userGameCurrency/export',
    {
      ...queryParams.value
    },
    `userGameCurrency_${new Date().getTime()}.xlsx`
  );
};

/** 导入按钮操作 */
const handleImport = () => {
  upload.title = 'TG用户游戏币导入';
  upload.open = true;
};

/** 下载模板操作 */
const importTemplate = () => {
  proxy?.download('mushcoin/userGameCurrency/importTemplate', {}, `userGameCurrency_template.xlsx`);
};

/**文件上传中处理 */
const handleFileUploadProgress = () => {
  upload.isUploading = true;
};
/** 文件上传成功处理 */
const handleFileSuccess = (response: any, file: UploadFile) => {
  upload.open = false;
  upload.isUploading = false;
  uploadRef.value?.handleRemove(file);
  ElMessageBox.alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + '</div>', '导入结果', {
    dangerouslyUseHTMLString: true
  });
  getList();
};

/** 提交上传文件 */
function submitFileForm() {
  uploadRef.value?.submit();
}

onMounted(() => {
  getList();
});

/** 排序触发事件(自带) */
const handleSortChange = (column: any) => {
  queryParams.value.orderByColumn = column.prop;
  queryParams.value.isAsc = column.order;
  getList();
};
</script>
